<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">

            <el-form-item label="姓名" prop="sportsmanId">
                <el-select
                        v-model="queryParams.sportsmanId"
                        placeholder="请选择姓名"
                        clearable
                        size="small"
                        style="width: 240px"
                >
                    <el-option
                            v-for="dict in sportsmanNameList"
                            :key="dict.id"
                            :label="dict.name"
                            :value="dict.id"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                >删除
                </el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="leaveFormList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="运动员" align="center" prop="sportsmanName"/>
            <el-table-column label="伤病类型" align="center" prop="type">
                <template slot-scope="scope">
                    <span>{{typeFormat(scope.row)}}</span><span style="color: red" v-if="scope.row.type==5">({{scope.row.reason}})</span>
                </template>
            </el-table-column>
            <el-table-column label="伤病时间" align="center" prop="startTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }} ~ {{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="队医" align="center" prop="teamDoctor"/>
            <el-table-column label="详情" align="center" prop="memo"/>
            <el-table-column label="队医诊断" align="center" prop="diagnosis"/>
            <el-table-column label="治愈时间" align="center" prop="cureTime" width="180">
                <template slot-scope="scope">
                    <span style="color: red" v-if="!scope.row.cureTime">治疗中</span>
                    <span style="color: green" v-if="scope.row.cureTime">已康复({{ parseTime(scope.row.cureTime, '{y}-{m}-{d}') }})</span>
                </template>
            </el-table-column>
            <!--<el-table-column label="凭证图片" align="center" prop="evidence">
                <template slot-scope="scope">

                </template>
            </el-table-column>-->
            <el-table-column label="创建时间" align="center" prop="ctime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            @click="handleCure(scope.row)"
                    >治愈
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改leaveForm对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">

                <el-form-item label="运动员" prop="sportsmanId">
                    <el-select
                            v-model="form.sportsmanId"
                            placeholder="请选择运动员"
                            clearable
                            size="small"
                            style="width: 240px"
                    >
                        <el-option
                                v-for="dict in sportsmanNameList"
                                :key="dict.id"
                                :label="dict.name"
                                :value="dict.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="请假类型" :formatter="typeFormat" prop="type">
                    <el-select
                            v-model="form.type"
                            placeholder="请选择请假类型"
                            clearable
                            size="small"
                            style="width: 240px"
                    >
                        <el-option
                                v-for="leaveType in leaveTypeList"
                                :key="leaveType.value"
                                :label="leaveType.name"
                                :value="leaveType.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="受伤部位" prop="reason" v-if="form.type==5">
                    <el-select
                            v-model="form.reason"
                            placeholder="请选择受伤部位"
                            clearable
                            size="small"
                            style="width: 240px"
                    >
                        <el-option
                                v-for="dict in reasonOptions"
                                :key="dict.dictLabel"
                                :label="dict.dictLabel"
                                :value="dict.dictLabel"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable size="small" style="width: 200px"
                                    v-model="form.startTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker clearable size="small" style="width: 200px"
                                    v-model="form.endTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="队医" prop="teamDoctor">
                    <el-select
                            v-model="form.teamDoctor"
                            placeholder="请选择队医"
                            clearable
                            size="small"
                            style="width: 240px"
                    >
                        <el-option
                                v-for="dict in sysUserList"
                                :key="dict.userId"
                                :label="dict.userName"
                                :value="dict.userName"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="详情" prop="memo">
                    <el-input v-model="form.memo" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="队医诊断" prop="diagnosis">
                    <el-input v-model="form.diagnosis" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="治愈时间" prop="cureTime">
                    <el-date-picker clearable size="small" style="width: 200px"
                                    v-model="form.cureTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择治愈时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="上传" prop="evidence">
                    <el-upload ref="evidence" :file-list="evidencefileList" :action="evidenceAction" multiple
                               :before-upload="evidenceBeforeUpload" :on-success="evidenceUploadSuccess" list-type="picture" accept="image/*">
                        <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        listLeaveForm,
        listGetSysUser,
        listSportsmanName,
        getLeaveForm,
        delLeaveForm,
        addLeaveForm,
        updateLeaveForm
    } from "@/api/training/leaveForm";

    export default {
        name: "LeaveForm",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 总条数
                total: 0,
                // leaveForm表格数据
                leaveFormList: [],
                //受伤部位
                reasonOptions: [],
                //请假类型字典
                leaveTypeOptions: [],
                leaveTypeList: [
                    {name: "受伤",value: 5},
                    {name: "生病",value: 4},
                    {name: "过敏",value: 6},
                ],
                //姓名集合
                sportsmanNameList: [],
                //队医集合
                sysUserList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    sportsmanId: undefined,
                    type: -1,
                    reason: undefined,
                    startTime: undefined,
                    endTime: undefined,
                    teamDoctor: undefined,
                    memo: undefined,
                    diagnosis: undefined,
                    cureTime: undefined,
                    ctime: undefined,
                    ltime: undefined
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    sportsmanId: [
                        {required: true, message: "运动员Id不能为空", trigger: "blur"}
                    ],
                    type: [
                        {required: true, message: "请假类型 1、伤病 2、病假 3、事假不能为空", trigger: "blur"}
                    ],
                    startTime: [
                        {required: true, message: "开始时间不能为空", trigger: "blur"}
                    ],
                    endTime: [
                        {required: true, message: "结束时间不能为空", trigger: "blur"}
                    ],
                },
                evidenceAction: '/api/common/upload',
                evidencefileList: [],
            };
        },
        created() {
            this.getList();
            this.getNameList();
            this.getSysUserList();
            this.getDicts("training_status_6").then(response => {
                this.leaveTypeOptions = response.data;
            });
            this.getDicts("injured_site").then(response => {
                this.reasonOptions = response.data;
            });
        },
        methods: {
            /** 查询leaveForm列表 */
            getList() {
                this.loading = true;
                listLeaveForm(this.queryParams).then(response => {
                    this.leaveFormList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            //赛事姓名集合
            getNameList() {
                this.loading = true;
                listSportsmanName().then(response => {
                    this.sportsmanNameList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });

            },

            //用户名称集合
            getSysUserList() {
                this.loading = true;
                listGetSysUser().then(response => {
                    this.sysUserList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },

            // 请假类型字典翻译
            typeFormat(row) {
                return this.selectDictLabel(this.leaveTypeOptions, row.type);
            },

            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    sportsmanId: undefined,
                    type: undefined,
                    reason: undefined,
                    startTime: undefined,
                    endTime: undefined,
                    teamDoctor: undefined,
                    memo: undefined,
                    diagnosis: undefined,
                    cureTime: undefined,
                    evidence: '',
                    ctime: undefined,
                    ltime: undefined
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length != 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加伤病";
                this.getNameList();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getLeaveForm(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改";
                    this.getNameList();
                });
            },
            evidenceBeforeUpload(file) {
                let isRightSize = file.size / 1024 / 1024 < 10
                if (!isRightSize) {
                    this.$message.error('文件大小超过 10MB')
                }
                let isAccept = new RegExp('image/*').test(file.type)
                if (!isAccept) {
                    this.$message.error('应该选择image/*类型的文件')
                }
                return isRightSize && isAccept
            },
            evidenceUploadSuccess(res, file) {
                // 如果上传成功
                if (res.code == 200) {
                    this.form.evidence += res.url;
                    this.form.evidence += ";";
                } else {
                    this.$message.error("图片插入失败");
                }
            },
            /** 提交按钮 */
            submitForm: function () {

                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != undefined) {
                            updateLeaveForm(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                } else {
                                    this.msgError(response.msg);
                                }
                            });
                        } else {
                            addLeaveForm(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                } else {
                                    this.msgError(response.msg);
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delLeaveForm(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                }).catch(function () {
                });
            },
            handleCure(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认已治愈', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {

                }).then(() => {
                    updateLeaveForm({
                        id:row.id,
                        sportsmanId:row.sportsmanId,
                        type:row.type,
                        reason: row.reason,
                        startTime: row.startTime,
                        endTime: row.endTime,
                        teamDoctor: row.teamDoctor,
                        memo: row.memo,
                        diagnosis: row.diagnosis,
                        evidence: row.evidence,
                        cureTime: this.parseTime(new Date(),"{y}-{m}-{d} {h}:{i}:{s}")
                    });
                    this.getList();
                    this.msgSuccess("确认成功");
                }).catch(function () {
                });
            },
        }
    };
</script>